package nero.yncc.com.createart.fragment;

import android.content.Intent;
import android.graphics.drawable.Icon;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentPagerAdapter;
import android.support.v4.view.ViewPager;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.ListAdapter;
import android.widget.ListView;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.handmark.pulltorefresh.library.PullToRefreshBase;
import com.handmark.pulltorefresh.library.PullToRefreshListView;

import org.xutils.view.annotation.ContentView;
import org.xutils.view.annotation.ViewInject;
import org.xutils.x;

import java.util.ArrayList;
import java.util.HashMap;

import nero.yncc.com.createart.R;
import nero.yncc.com.createart.activity.ActivityArticleList;
import nero.yncc.com.createart.activity.ActivityMore;
import nero.yncc.com.createart.activity.ActivitySearch;
import nero.yncc.com.createart.activity.ActivitySearchAllGod;
import nero.yncc.com.createart.activity.ActivitySearchChangjing;
import nero.yncc.com.createart.activity.ActivityShopcar;
import nero.yncc.com.createart.adapter.AdapterIndexChangjing;
import nero.yncc.com.createart.adapter.AdapterIndexFound;
import nero.yncc.com.createart.adapter.AdapterIndexHotSale;
import nero.yncc.com.createart.adapter.AdapterIndexNewGodRecyler;
import nero.yncc.com.createart.adapter.AdapterIndexTejia;
import nero.yncc.com.createart.base.BaseFragment;
import nero.yncc.com.createart.base.Constants;
import nero.yncc.com.createart.bean.BeanAd;
import nero.yncc.com.createart.bean.BeanArticle;
import nero.yncc.com.createart.bean.BeanIndex;
import nero.yncc.com.createart.bean.ChangjingBean;
import nero.yncc.com.createart.bean.GodBean;
import nero.yncc.com.createart.bean.TejiaBean;
import nero.yncc.com.createart.custom.IconView;
import nero.yncc.com.createart.custom.MyRecyclerView;
import nero.yncc.com.createart.utils.ArrayUtils;
import nero.yncc.com.createart.utils.HttpUtils;
import nero.yncc.com.createart.utils.ShopCarUtil;

/**
 * Created by nero on 17-6-22.
 */

@ContentView(R.layout.fragment_index)
public class FragmentIndex extends BaseFragment {
    @ViewInject(R.id.iv_nav_new)
    ImageView iv_nav_new;
    @ViewInject(R.id.tv_nav_new)
    TextView tv_nav_new;
    @ViewInject(R.id.iv_nav_te)
    ImageView iv_nav_te;
    @ViewInject(R.id.tv_nav_te)
    TextView tv_nav_te;

    @ViewInject(R.id.vp_main)
    ViewPager vp_main;

    @ViewInject(R.id.ll_dom)
    LinearLayout ll_dom;

    @ViewInject(R.id.rl_dom)
    RelativeLayout rl_dom;

//    @ViewInject(R.id.ll_dom_big)
//    LinearLayout ll_dom_big;
//
//    @ViewInject(R.id.rl_dom_big)
//    RelativeLayout rl_dom_big;

    @ViewInject(R.id.ll_search)
    LinearLayout ll_search;

    @ViewInject(R.id.rv_found)
    MyRecyclerView rv_found;

    @ViewInject(R.id.rv_changjing)
    ListView rv_changjing;

    @ViewInject(R.id.rv_newgod)
    RecyclerView rv_newgod;

    @ViewInject(R.id.rv_tuijian)
    ListView rv_tuijian;

    @ViewInject(R.id.pt_hotsale)
    PullToRefreshListView pt_hotsale;

    @ViewInject(R.id.icon_shopcar)
    IconView icon_shopcar;


    @ViewInject(R.id.iv_changjing_banner)
    ImageView iv_changjing_banner;   //banner 场景图
    @ViewInject(R.id.tv_changjing_banner)
    TextView tv_changjing_banner;

    @ViewInject(R.id.tv_allgod_banner)
    TextView tv_allgod_banner;      //banner 所有商品
    @ViewInject(R.id.iv_allgod_banner)
    ImageView iv_allgod_banner;

    @ViewInject(R.id.et_search)
    TextView et_search;


    @ViewInject(R.id.tv_more_found)
    TextView tv_more_found;
    @ViewInject(R.id.tv_more_changjing)
    TextView tv_more_changjing;
    @ViewInject(R.id.tv_more_hot)
    TextView tv_more_hot;
    @ViewInject(R.id.tv_more_tejia)
    TextView tv_more_tejia;
    @ViewInject(R.id.rl_more_new)
    RelativeLayout rl_more_new;

    View view_red;

    ArrayList<BeanAd> arrayList_ad;

    //发现
    AdapterIndexFound adapterIndexFound;
    ArrayList<BeanArticle> arrayList_found;//文章数组

    //场景套餐
    ArrayList<ChangjingBean> arrayList_changjing;
    AdapterIndexChangjing adapterIndexChangjing;

    //本周新品
    AdapterIndexNewGodRecyler adapterIndexNewGod;
    ArrayList<GodBean.Goods> arrayList_newGod;//
    //特价
    AdapterIndexTejia adapterIndexTejia;
    ArrayList<TejiaBean> arrayList_tejia;
    //本周热销
    AdapterIndexHotSale adapterIndexHotSale;
    ArrayList<GodBean.Goods> arrayList_hot;//

    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        View view = x.view().inject(this, inflater, container);

        view = byScrollRefresh(view, true);
        initAll();

        ll_search.setFocusable(true);
        ll_search.setFocusableInTouchMode(true);
        ll_search.requestFocus();
        return view;
    }


    @Override
    protected void initView() {
//        ll_search.setPadding(0, getStateBarHeight() + 10, 0, 0);
        LinearLayoutManager manager = new LinearLayoutManager(mContext, LinearLayoutManager.HORIZONTAL, false);
        rv_newgod.setLayoutManager(manager);

//        scrollView.setListView(pt_hotsale);
    }

    @Override
    protected void initData() {
        arrayList_ad = new ArrayList<>();
        //发现
        arrayList_found = new ArrayList<>();
        adapterIndexFound = new AdapterIndexFound(mContext, arrayList_found);
        LinearLayoutManager llm = new LinearLayoutManager(mContext);
        llm.setOrientation(LinearLayoutManager.HORIZONTAL);
        rv_found.setLayoutManager(llm);
        rv_found.setAdapter(adapterIndexFound);

        //场景套餐
        arrayList_changjing = new ArrayList<>();
        adapterIndexChangjing = new AdapterIndexChangjing(mContext, arrayList_changjing);

        rv_changjing.setAdapter(adapterIndexChangjing);

        //本周新品
        arrayList_newGod = new ArrayList<>();
        adapterIndexNewGod = new AdapterIndexNewGodRecyler(mContext, arrayList_newGod);
        rv_newgod.setAdapter(adapterIndexNewGod);

        //特价
        arrayList_tejia = new ArrayList<>();
        adapterIndexTejia = new AdapterIndexTejia(mContext, arrayList_tejia);
        rv_tuijian.setAdapter(adapterIndexTejia);

        //热销
        arrayList_hot = new ArrayList<>();
        adapterIndexHotSale = new AdapterIndexHotSale(mContext, arrayList_hot);
        pt_hotsale.setMode(PullToRefreshBase.Mode.BOTH);
        pt_hotsale.setAdapter(adapterIndexHotSale);
    }

    @Override
    protected void getHttpData() {
        super.getHttpData();

        HashMap<String, String> hashMap = new HashMap<>();
        hashMap.put("user_id", userBean.getUser_id());
        HttpUtils.get(getActivity(), Constants.API_INDEX, hashMap, BeanIndex.class, HttpUtils.TYPE_OBJECT, new HttpUtils.HttpCallback() {
            @Override
            public void success(String msg, Object json) {
                BeanIndex bean = (BeanIndex) json;

                ArrayUtils.copy(bean.getRexiao(), arrayList_hot, true);
                ArrayUtils.copy(bean.getAd(), arrayList_ad, true);
                ArrayUtils.copy(bean.getXinpin(), arrayList_newGod, true);
                ArrayUtils.copy(bean.getDiscover(), arrayList_found, true);
                ArrayUtils.copy(bean.getTejia(), arrayList_tejia, true);
                ArrayUtils.copy(bean.getChangjing(), arrayList_changjing, true);

                adapterIndexChangjing.notifyDataSetChanged();
                adapterIndexFound.notifyDataSetChanged();
                adapterIndexHotSale.notifyDataSetChanged();
                adapterIndexNewGod.notifyDataSetChanged();
                adapterIndexTejia.notifyDataSetChanged();
                adapterIndexChangjing.notifyDataSetChanged();

                initViewPager();

                setListViewHeight(pt_hotsale);
                loadOverShow();
            }

            @Override
            public void fail(String failMsg) {
                showToast("请求数据失败，请检查网络链接");
            }
        });

//        ShopCarUtil.getShopCarNum(getActivity(), userBean, new ShopCarUtil.ShopCarGetListener() {
//            @Override
//            public void success(String num) {
//                showToast(num);
//            }
//
//            @Override
//            public void fail(String num) {
//                showToast(num);
//            }
//        });

    }

    private void initViewPager() {
        final int page = arrayList_ad.size();//总的viewpage数量

        if (page > 1) {
            for (int a = 0; a < page; a++) {
                View views = LinearLayout.inflate(mContext, R.layout.view_dom, null);
                LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(15, 15);
                params.setMargins(5, 5, 5, 5);
                views.setLayoutParams(params);
                ll_dom.addView(views);
            }

            view_red = LinearLayout.inflate(mContext, R.layout.view_dom, null);
            RelativeLayout.LayoutParams params = new RelativeLayout.LayoutParams(15, 15);
            params.setMargins(5, 5, 5, 5);
            view_red.setLayoutParams(params);
            view_red.setBackgroundResource(R.drawable.tv_dom);
            params.addRule(RelativeLayout.ALIGN_LEFT, R.id.ll_dom);
            rl_dom.addView(view_red);

            vp_main.addOnPageChangeListener(new ViewPager.OnPageChangeListener() {
                @Override
                public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {

                    RelativeLayout.LayoutParams params = (RelativeLayout.LayoutParams) view_red.getLayoutParams();
                    //70为一个

                    params.setMargins((int) (25 * (positionOffset) + 25 * position) + 5, 5, 5, 5);
                    view_red.setLayoutParams(params);
                    view_red.setBackgroundResource(R.drawable.tv_dom);
                }

                @Override
                public void onPageSelected(int position) {
                }

                @Override
                public void onPageScrollStateChanged(int state) {

                }
            });

        }

        vp_main.setAdapter(new FragmentPagerAdapter(this.getChildFragmentManager()) {
            @Override
            public Fragment getItem(int position) {
                FragmentAdIndex fragmentAd = new FragmentAdIndex();
                Bundle bundle = new Bundle();
                bundle.putSerializable("ad", arrayList_ad.get(position));
                fragmentAd.setArguments(bundle);
                return fragmentAd;
            }

            @Override
            public int getCount() {
                return page;
            }
        });
    }

    @Override
    protected void initListener() {
        super.initListener();

        iv_changjing_banner.setOnClickListener(this);
        tv_changjing_banner.setOnClickListener(this);

        tv_allgod_banner.setOnClickListener(this);
        iv_allgod_banner.setOnClickListener(this);
        et_search.setOnClickListener(this);

        tv_more_found.setOnClickListener(this);
        tv_more_changjing.setOnClickListener(this);
        tv_more_hot.setOnClickListener(this);
        tv_more_tejia.setOnClickListener(this);
        rl_more_new.setOnClickListener(this);
        icon_shopcar.setOnClickListener(this);

        tv_nav_new.setOnClickListener(this);
        iv_nav_new.setOnClickListener(this);
        tv_nav_te.setOnClickListener(this);
        iv_nav_te.setOnClickListener(this);
    }

    @Override
    protected void doClick(int id) {
        super.doClick(id);
        Intent intent;
        switch (id) {
            case R.id.iv_changjing_banner:
                openActivity(ActivitySearchChangjing.class, null);
                break;
            case R.id.tv_changjing_banner:
                openActivity(ActivitySearchChangjing.class, null);
                break;
            case R.id.iv_allgod_banner:
                openActivity(ActivitySearchAllGod.class, null);
                break;
            case R.id.tv_allgod_banner:
                openActivity(ActivitySearchAllGod.class, null);
                break;
            case R.id.et_search:
                intent = new Intent(mContext, ActivitySearch.class);
                mContext.startActivity(intent);
                break;
            case R.id.tv_more_found:   //发现 新品 热销 特价
//                intent = new Intent(mContext, ActivityMore.class);
//                intent.putExtra("type", "发现");
//                startActivity(intent);
                if (TextUtils.isEmpty(Constants.hashmap_type.get("发现"))) {
                    showToast("尚未添加该分类");
                    return;
                }

                Intent intent_bt = new Intent(mContext, ActivityArticleList.class);
                intent_bt.putExtra("id",Constants.hashmap_type.get("发现"));
                intent_bt.putExtra("title","发现");
                startActivity(intent_bt);
                break;
            case R.id.tv_more_changjing:   //发现 新品 热销 特价
                intent = new Intent(mContext, ActivitySearchChangjing.class);
                startActivity(intent);
                break;

            case R.id.tv_more_hot:   //发现 新品 热销 特价
                intent = new Intent(mContext, ActivityMore.class);
                intent.putExtra("type", "本周热销");
                startActivity(intent);
                break;

            case R.id.tv_more_tejia:   //发现 新品 热销
                intent = new Intent(mContext, ActivityMore.class);
                intent.putExtra("type", "特价");
                startActivity(intent);

                break;
            case R.id.rl_more_new:   //发现 新品 热销 特价
                intent = new Intent(mContext, ActivityMore.class);
                intent.putExtra("type", "新品");
                startActivity(intent);

                break;
            case R.id.icon_shopcar:
                startActivity(new Intent(mContext,ActivityShopcar.class));
                break;
            case R.id.tv_nav_new:
                intent = new Intent(mContext, ActivityMore.class);
                intent.putExtra("type", "新品");
                startActivity(intent);
                break;
            case R.id.iv_nav_new:
                intent = new Intent(mContext, ActivityMore.class);
                intent.putExtra("type", "新品");
                startActivity(intent);
                break;
            case R.id.tv_nav_te:
                intent = new Intent(mContext, ActivityMore.class);
                intent.putExtra("type", "特价");
                startActivity(intent);
                break;
            case R.id.iv_nav_te:
                intent = new Intent(mContext, ActivityMore.class);
                intent.putExtra("type", "特价");
                startActivity(intent);
                break;
        }
    }


    public void setListViewHeight(PullToRefreshListView lv) {
        if (lv == null) {
            return;
        }
        ListAdapter adapter = lv.getRefreshableView().getAdapter();
        if (adapter == null) {
            return;
        }
        int totalHeight = 0;
        for (int i = 0; i < adapter.getCount(); i++) {
            View listItem = adapter.getView(i, null, lv);
            listItem.measure(0, 0);
            totalHeight += listItem.getMeasuredHeight();
        }
        ViewGroup.LayoutParams params = lv.getLayoutParams();
        params.height = totalHeight + (lv.getRefreshableView().getDividerHeight() * (lv.getRefreshableView().getCount() - 1));//这里还将分割线的高度考虑进去了，统计出所有分割线占有的高度和
        lv.setLayoutParams(params);
    }
}
